package com.hb.game.pk.ui.activity.game;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hb.game.pk.R;
import com.hb.game.pk.adpter.GameRoomListAdapter;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.RoomList;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;
import com.google.gson.Gson;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GameRoomActivity extends BaseActivity<RoomList> {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    String type;
    List<RoomList.DataBean> data;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_game_room;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.rv_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GameRoomActivity.this.finish();
            }
        });
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).rooms(type);
        call.enqueue(new Callback<RoomList>() {
            @Override
            public void onResponse(Call<RoomList> call, Response<RoomList> response) {
                RoomList body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                data = body.getData();
                initAdapter();
            }

            @Override
            public void onFailure(Call<RoomList> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }

    @Override
    protected void initAdapter() {
        if (mAdapter == null) {
            mAdapter = new GameRoomListAdapter(R.layout.item_game_room);
            mAdapter.setNewData(data);
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    RoomList.DataBean dataBean = data.get(position);
                    Intent intent = new Intent();
                    intent.putExtra("room", new Gson().toJson(dataBean));
                    if ("saolei".equals(type)) {
                        startActivity(SaoLeiChatActivity1.class, intent);
                    } else if ("jielong".equals(type)) {
                        startActivity(JieLongChatActivity1.class, intent);
                    } else if ("erba".equals(type)) {
                        startActivity(ErBaChatActivity.class, intent);
                    }else if ("szww".equals(type)){
                        startActivity(SZWWChatActivity.class, intent);
                    }
                }
            });
        } else {
            mAdapter.setNewData(data);
        }
        super.initAdapter();
    }

    @Override
    public boolean isNeedLoadMore() {
        return false;
    }
}
